﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLToolkit.Common;
using System.Data;
using BLToolkit.Data.Sql;

namespace Platform.Model.Repositories
{
    public class StoreRepository
    {
        public static List<Store> GetAll()
        {
            return Store.Objects.GetAll().ToList();
        }
        public static Store GetById(int id)
        {
            return Store.Objects.Single(x => x.Id == id);
        }
        public static int Insert(Store store)
        {
            // Store moi nhung co StoreNo trung voi Store cu
            // khong them duoc => thong bao loi
            if (store.Id == 0 && isExistedStoreNo(store.StoreNo))
                throw new DuplicateNameException("Cannot add new store with duplicate STORE_NO");

            // Them moi store
            if (store.Id == 0)
            {
                store.Insert();
            }
            return store.Id;
        }

        /// <summary>
        /// Kiem tra storeNo da ton tai chua
        /// </summary>
        /// <param name="storeNo"></param>
        /// <returns></returns>
        public static bool isExistedStoreNo(String storeNo)
        {
            Store store = null;

            //tim store theo storeNo
            store = Store.Objects.FirstOrDefault(x => x.StoreNo == storeNo);

            //neu tim thay store => da ton tai storeNo => return true
            if (store != null)
            {
                return true;
            }

            return false;
        }
        public static bool Update(Store store)
        {
            return Store.Objects.Update(store);
        }
        public static void DeleteByKey(int storeId)
        {
            Store.Objects.DeleteByKey(storeId);
        }
        public static void Delete(Store store)
        {
            //co the dung try catch de throw SqlException => don gian hon
           
            //lay ds pickingList tham chieu den store
            List<PickingList> pickingLists = getPickingListsReferenceToStore(store);

            //neu pickingLists.Count == 0 => ko co pickingList nao tham chieu den Store => xoa duoc
            if (pickingLists.Count == 0)
            {
                Store.Objects.Delete(store);
            }
            else {
                throw new SqlException("Cannot delete this store beacause constrain with foreign key reference by pickingList!"); 
            }
        }
        
        /// <summary>
        /// tim ds pickingList tham chieu den store
        /// </summary>
        /// <param name="store"></param>
        /// <returns></returns>
        public static List<PickingList> getPickingListsReferenceToStore(Store store)
        {
            List<PickingList> pickingLists = null;

            //tim ds pickingList tham chieu den store
            pickingLists = PickingList.Objects.ToList().FindAll(x => x.StoreId == store.Id);

            return pickingLists;
        }
    }
}
